package com.leetcode.根据数据结构分类.数组;

/**
 * @author: xiaomi
 * @date: 2021/1/17
 * @description: 1232. 缀点成线
 * https://leetcode-cn.com/problems/check-if-it-is-a-straight-line/
 */
public class A_1232_缀点成线 {

    public static void main(String[] args) {
        //int[][] coordinates = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}};
//        int[][] coordinates = {{1, -8}, {2, -3}, {1, 2}};
        int[][] coordinates = {{4,8},{-2,8},{1,8},{8,8},{-5,8},{0,8},{7,8},{5,8}};

        A_1232_缀点成线 action = new A_1232_缀点成线();
        boolean res = action.checkStraightLine(coordinates);
        System.out.println("res = " + res);
    }

    /**
     * 一看就是求斜率
     * O(n) 复杂度
     *
     * @param coordinates
     * @return
     */
    public boolean checkStraightLine(int[][] coordinates) {
        //Double.POSITIVE_INFINITY
        double slope = getSlope(coordinates[1], coordinates[0]);
        int rowNum = coordinates.length;
        for (int i = 2; i < rowNum; i++) {
            double tempSlope = getSlope(coordinates[i], coordinates[0]);
            if (tempSlope != slope) {
                return false;
            }
        }
        return true;
    }

    double getSlope(int[] p1, int[] p2) {
        if (p1[1] == p2[1]){
            return Double.POSITIVE_INFINITY;
        }
        return (double) (p1[0] - p2[0]) / (double) (p1[1] - p2[1]);
    }
}
